<%@ page pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/view/inc/taglibs.jsp" %>
<%@ page import="java.util.HashMap,java.util.List,java.util.Iterator" %>
<%@ page import="org.apache.jcs.*,org.apache.jcs.admin.*" %>
<%@ page import="org.apache.jcs.engine.behavior.ICacheElement" %>
<jsp:useBean id="jcsBean" scope="request" class="org.apache.jcs.admin.JCSAdminBean" />
<!doctype html>
<html>
<head>
<%@ include file="/WEB-INF/view/inc/meta.jsp" %>
<title>系统缓存对象管理</title>
<style type="text/css">
body {
	text-align: center;
}
#page {
	width: 800px; border:1px solid #92B0DD; margin: 20px auto;
}
#page h1 {
	margin: 1px; background-color: #E2EAF8;
}

table,td {   
    font: 100% Arial, Helvetica, sans-serif;   
}   
  
table {   
    width: 100%;   
    border-collapse: collapse;   
    margin: 1em 0;   
}   
th,td {   
    text-align: left;   
    padding: .3em;   
    border: 1px solid #fff;   
}
th {
    background: #328aa4 repeat-x;
    color: #fff;   
}   

td {   
    background: #e5f1f4;
}   
  
tr.even td {   
    background: #e5f1f4;   
}   
  
tr.odd td {   
    background: #f8fbfc;   
}   
  
th.over,tr.even th.over,tr.odd th.over {   
    background: #4a98af;   
}   
  
th.down,tr.even th.down,tr.odd th.down {   
    background: #bce774;   
}   
  
th.selected,tr.even th.selected,tr.odd th.selected {   
	
}
  
td.over,tr.even td.over,tr.odd td.over {   
    background: #ecfbd4;   
}   
  
td.down,tr.even td.down,tr.odd td.down {   
    background: #bce774;   
    color: #fff;   
}   

td.selected,tr.even td.selected,tr.odd td.selected {   
    background: #bce774;   
    color: #555;   
}   
  
/* use this if you want to apply different styleing to empty table cells*/   
td.empty,tr.odd td.empty,tr.even td.empty {   
    background: #fff;   
}   

</style>
</head>

<body>
<div id="page">
<%
String CACHE_NAME_PARAM = "cacheName";
String ACTION_PARAM = "action";
String CLEAR_ALL_REGIONS_ACTION = "clearAllRegions";
String CLEAR_REGION_ACTION = "clearRegion";
String REMOVE_ACTION = "remove";
String DETAIL_ACTION = "detail";
String REGION_SUMMARY_ACTION = "regionSummary";
String ITEM_ACTION = "item";
String KEY_PARAM = "key";
String SILENT_PARAM = "silent";

String DEFAULT_TEMPLATE_NAME = "DEFAULT";
String REGION_DETAIL_TEMPLATE_NAME = "DETAIL";
String ITEM_TEMPLATE_NAME = "ITEM";
String REGION_SUMMARY_TEMPLATE_NAME = "SUMMARY";
int iclass = 0; 
String classStr = "";
String templateName = DEFAULT_TEMPLATE_NAME;

HashMap context = new HashMap();

// Get cacheName for actions from request (might be null)
String cacheName = request.getParameter( CACHE_NAME_PARAM );

if (cacheName != null) {
	cacheName = cacheName.trim();
}

// If an action was provided, handle it
String action = request.getParameter( ACTION_PARAM );

if (action != null) {
	if ( action.equals( CLEAR_ALL_REGIONS_ACTION ) ) {
		// 清除所有区域缓存信息
		jcsBean.clearAllRegions();
		
	} else if ( action.equals( CLEAR_REGION_ACTION ) ) {
		
		if ( cacheName == null ) {
			// Not Allowed
		} else {
			// 清除指定的缓存区域信息
			jcsBean.clearRegion( cacheName );
		}
		
	} else if ( action.equals( REMOVE_ACTION ) ) {
		
		String[] keys = request.getParameterValues( KEY_PARAM );
		for ( int i = 0; i < keys.length; i++ ) {
			// 删除指定缓存区域中指定的缓存对象
			jcsBean.removeItem( cacheName, keys[ i ] );
		}
	
		templateName = REGION_DETAIL_TEMPLATE_NAME;
		
	} else if ( action.equals( DETAIL_ACTION ) ) {
		
		templateName = REGION_DETAIL_TEMPLATE_NAME;
		
	} else if ( action.equals( ITEM_ACTION ) ) {
		
		templateName = ITEM_TEMPLATE_NAME;
		
	} else if ( action.equals( REGION_SUMMARY_ACTION ) ) {
		
		templateName = REGION_SUMMARY_TEMPLATE_NAME;
		
	}
}

if ( request.getParameter( SILENT_PARAM ) != null ) {
	
	// If silent parameter was passed, no output should be produced.
	//return null;
	
} else {
	// Populate the context based on the template
	if ( templateName == REGION_DETAIL_TEMPLATE_NAME ) {
		
		//context.put( "cacheName", cacheName );
		context.put( "elementInfoRecords", jcsBean.buildElementInfo( cacheName ) );
		
	} else if ( templateName == DEFAULT_TEMPLATE_NAME ) {
		
		context.put( "cacheInfoRecords", jcsBean.buildCacheInfo() );
		
	}
}

///////////////////////////////////////////////////////////////////////////////////
//handle display
if ( templateName == ITEM_TEMPLATE_NAME ) {
    String key = request.getParameter( KEY_PARAM );
    if ( key != null ) {
        key = key.trim();
	}
    JCS cache = JCS.getInstance( cacheName );
	ICacheElement element = cache.getCacheElement( key );
%>

<h1>缓存对象key：[<%=key%>]在缓存区域[<%=cacheName%>]</h1>
<a href="CacheAdmin.jsp?action=detail&cacheName=<%=cacheName%>">缓存区域详情</a>|<a href="CacheAdmin.jsp">所有缓存区域</a>
<pre>
	<%=element%>
</pre>

<%
} else if ( templateName == REGION_SUMMARY_TEMPLATE_NAME ) {
%>

<h1>[<%=cacheName%>]缓存区域信息简介</h1>
<a href="CacheAdmin.jsp">所有缓存区域</a>

<%
    JCS cache = JCS.getInstance( cacheName );
    String stats = cache.getStats();
%>

<br>
<b>[<%=cacheName%>]缓存区域状态</b>
<pre>
	<%=stats%>
</pre>

<%
} else if ( templateName == REGION_DETAIL_TEMPLATE_NAME ) {
%>

<h1>[<%=cacheName%>]缓存区域信息详细</h1>
<a href="CacheAdmin.jsp">所有缓存区域</a>
<table border="0" cellpadding="0" cellspacing="1" class="dataContent">
	<tr class="dataHeadTr">
		<td>键值</td>
		<td>内容</td>
		<td>创建时间</td>
		<td>最大有效期(s) </td>
		<td>距离失效时间</td>
		<td>操作</td>
    </tr>
<%
	List list = (List)context.get( "elementInfoRecords" );
    Iterator it = list.iterator();
    while ( it.hasNext() ) {
		iclass++;
    	CacheElementInfo element = (CacheElementInfo)it.next();
    	if(iclass%2 == 0){
     		classStr = "dataEvenTr";
     	} else { 
       		classStr = "dataOddTr";
       	}
%>
	<tr class="<%=classStr%>">
	    <td><%=element.getKey()%></td>
	    <td><%=element.isEternal()%></td>
	    <td><%=element.getCreateTime()%></td>
	    <td><%=element.getMaxLifeSeconds()%></td>
	    <td><%=element.getExpiresInSeconds()%></td>
	    <td>
	    	<a href="CacheAdmin.jsp?action=item&cacheName=<%=cacheName%>&key=<%=element.getKey()%>">查看</a>|
	    	<a href="CacheAdmin.jsp?action=remove&cacheName=<%=cacheName%>&key=<%=element.getKey()%>">删除</a>
	    </td>
	</tr>
<%
    } /* ----while end---- */
    
    JCS cache = JCS.getInstance( cacheName );
    String stats = cache.getStats();
%>
</table>
<br>

<b>[<%=cacheName%>]缓存区域状态</b>
<pre>
	<%=stats%>
</pre>

<%
  } else {
%>

<h1>缓存区域</h1>
<p>
这是在这台机器上面所有的缓存配置信息. 'Items' 和 'Bytes' 指的是内存中的数据，不包含硬盘中的数据.
你可以使用 清除链接 清除某个缓存区域中所以的缓存对象.
你也可以<a href="javascript:decision('Clicking OK will clear all the data from all regions!','CacheAdmin.jsp?action=clearAllRegions')">清除所有</a>缓存区域的缓存信息.
</p>
<form action="CacheAdmin.jsp">
	<input type="hidden" name="action" value="item">
	键值<input type="text" name="key">&nbsp;
	区域<select name="cacheName">
<%
	List listSelect = (List)context.get( "cacheInfoRecords" );
	Iterator itSelect = listSelect.iterator();
	while ( itSelect.hasNext() ) {
		CacheRegionInfo record = (CacheRegionInfo)itSelect.next();
%>
		<option value="<%=record.getCache().getCacheName()%>"><%=record.getCache().getCacheName()%></option>
<%
	}
%>
	</select>
	<input type="submit" value="查询">
</form>
<br/>
<table border="0" cellpadding="0" cellspacing="1" class="dataContent">
	<colgroup>
		<col id="r"/>
		<col id="g"/>
		<col id="e"/>
		<col id="f"/>
	</colgroup>
	<thead>
		<tr>
			<th>缓存区域</th>
			<th>键值数</th>
			<th>缓存对象字节大小</th>
			<th>Status</th>
			<th>内存缓存命中</th>
			<th>Aux Hits</th>
			<th>未在内存缓存中命中</th>
			<th>Expired Misses</th>
			<th>操作</th>
		</tr>
 	</thead>
<%
	List list = (List)context.get( "cacheInfoRecords" );
    Iterator it = list.iterator();
    while (it.hasNext() ) {
    	iclass++;
		CacheRegionInfo record = (CacheRegionInfo)it.next();
    	if(iclass%2 == 0){
			classStr = "dataEvenTr";
		} else {
       		classStr = "dataOddTr";
       	}
%>
	<tr class="<%=classStr %>">
	    <td><%=record.getCache().getCacheName()%></td>
	    <td><%=record.getCache().getSize()%></td>
	    <td><%=record.getByteCount()%></td>
	    <td><%=record.getStatus()%></td>
	    <td><%=record.getCache().getHitCountRam()%></td>
	    <td><%=record.getCache().getHitCountAux()%></td>
	    <td><%=record.getCache().getMissCountNotFound()%></td>
	    <td><%=record.getCache().getMissCountExpired()%></td>
	    <td>
	    	<a href="CacheAdmin.jsp?action=regionSummary&cacheName=<%=record.getCache().getCacheName()%>">简要</a>|
	        <a href="CacheAdmin.jsp?action=detail&cacheName=<%=record.getCache().getCacheName()%>">详情</a>|
	        <a href="javascript:decision('点击确定键清除缓存区域[<%=record.getCache().getCacheName()%>]的所有缓存信息!','CacheAdmin.jsp?action=clearRegion&cacheName=<%=record.getCache().getCacheName()%>')">清除</a>
	    </td>
	</tr>
<%
    } /* ----while end---- */
%>
</table>
<%
} /* ----else end---- */
%>
</div>
<script>
function decision(message, url) {
	if(confirm(message)) {
		location.href = url;
	}
}
</script>
</body>
</html>
